﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections.Concurrent;
using UIDATALayer;
namespace UserData
{
    public enum LogType
    {
        Info,
        Warn,
        Err
    }
    public struct LogValue
    {
        public string value;
        public LogType logType;
        public LogValue(string value,LogType logType)
        {
            this.value = value;
            this.logType = logType;
        }
    }
    public partial class RunLoggeHelp : UserControl,Entity.Interface_UserControl
    {
        private UIDATALayer.UIDataAction<LogData> acLog = null;
        string str_StationName = "";
        [Category("工站名称,所属工站名称包含此字符，即会在此工位打印Log")]
        public string 工站名称
        {
            set { str_StationName = value; }
            get { return str_StationName; }
        }
        [Category("Log字体")]
        public Font 字体
        {
            set { richTextBox1.Font = value;}
            get { return richTextBox1.Font; }
        }
        int n_Max行数 = 500;
        [Category("最大行数")]
        public int 最大行数
        {
            set { n_Max行数 = value; }
            get { return n_Max行数; }
        }
        int n_保留行数 = 200;
        [Category("保留行数")]
        public int 最大保留行数
        {
            set { n_保留行数 = value; }
            get { return n_保留行数; }
        }
        public RunLoggeHelp()
        {
            InitializeComponent();
            
        }
        public void Init()
        {
            LogHelper.OnTest_ShowLog += Event_Log;
            Event_Log(工站名称, "软件加载成功");
        }
        private void Event_Log(string stationName, string value, LogType logType = LogType.Info)
        {
            if(acLog==null)
            {
                UIDATALayerHelper.RegistAction<LogData>(nameof(LogData), FnDoLog).Start();
                acLog = UIDATALayer.UIDATALayerHelper.GetRegistAction<LogData>(nameof(LogData));
                acLog.SetQueueNum(100);
            }
            if (stationName == 工站名称)
            {
                LogData data = new LogData();
                data.stationName = stationName;
                data.value = value;
                data.logType = logType;
                acLog.SetData(data);
            }
        }
        private void FnDoLog(LogData data)
        {
            if (InvokeRequired)
            {
                this.BeginInvoke(new Action(() => FnDoLog(data)));
            }
            else
            {
                if (str_StationName == "")
                    return;
                if (!data.stationName.Contains(str_StationName))
                    return;
                Color color = Color.Black;
                switch (data.logType)
                {
                    case LogType.Info:
                        color = Color.Black;
                        break;
                    case LogType.Warn:
                        color = Color.Blue;
                        break;
                    case LogType.Err:
                        color = Color.Red;
                        break;
                }
                richTextBox1.SelectionStart = richTextBox1.TextLength;
                richTextBox1.SelectionLength = 0;
                richTextBox1.SelectionColor = color;
                richTextBox1.AppendText($"{DateTime.Now.ToString("HH:mm:ss:fff")} {data.value}\r\n");
                richTextBox1.SelectionColor = richTextBox1.ForeColor;
                richTextBox1.ScrollToCaret();
                if (richTextBox1.Lines.Length > n_Max行数)
                {
                    int moreline = richTextBox1.Lines.Length - n_保留行数;
                    string[] lines = richTextBox1.Lines;
                    Array.Copy(lines, moreline, lines, 0, n_保留行数);
                    Array.Resize(ref lines, n_保留行数);
                    richTextBox1.Lines = lines;
                }
                Function_LogSava(data.value, data.logType);
            }
        }

        private object lockObject = new object();
        private void Function_LogSava(string meg, LogType type = LogType.Info)
        {
            Task.Run(() =>
            {
                try
                {
                    BaseDll.DeviceDataCollect.GetInstance().Function_RuntimeLog(str_StationName, meg, type.ToString());
                }
                catch (Exception)
                {


                }
            });
        }
        BlockingCollection<LogValue> blockingCollection = new BlockingCollection<LogValue>();


        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void 清除日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }

        private void RunLoggeHelp_Load(object sender, EventArgs e)
        {
        }
    }
}
